<template>
  <el-tabs v-model="activeName"  type="card" class="app-container" @tab-click="tabClick">
    <el-tab-pane label="品种管理" name="first">
      <div class="app-container">
        <el-row :gutter="10" class="top-right-btn mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              :disabled="multiple"
              @click="handleAdd"
              v-hasPermi="['work:variety:add']"
            >保存</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['work:variety:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['work:variety:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['work:variety:export']"
            >导出</el-button>
          </el-col>

        </el-row>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="名称" prop="varietyName">
            <el-input
              v-model="queryParams.varietyName"
              placeholder="请输入名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="编号" prop="varietyNum">
            <el-input
              v-model="queryParams.varietyNum"
              placeholder="请输入编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
<!--        <div class="top-right-btn">-->

<!--        </div>-->
        <el-table v-loading="loading" :data="varietyList" @selection-change="handleSelectionChange" >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="品种id"  align="center" prop="varietyId" @click.native="addLine()" v-if="false">
            <template scope="scope">
              <el-input size="small" v-model="scope.row.varietyId" placeholder="请输入内容" @click.native="addLine()"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="编号" align="center" prop="varietyNum" >
            <template scope="scope">
              <el-input @click.native="addLine()" size="small" v-model="scope.row.varietyNum" placeholder="请输入内容" ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="名称" align="center" prop="varietyName" >
            <template scope="scope">
              <el-input size="small" v-model="scope.row.varietyName" placeholder="请输入内容" ></el-input>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleNewUpdate(scope.row)"
                v-hasPermi="['work:variety:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleRowDelete(scope.$index, scope.row)"
                v-hasPermi="['work:variety:remove']"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改品种管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="名称" prop="varietyName">
              <el-input v-model="form.varietyName" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="编号" prop="varietyNum">
              <el-input v-model="form.varietyNum" placeholder="请输入编号" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </el-tab-pane>
    <el-tab-pane label="区域管理" name="second">
      <div class="app-container">
        <el-row :gutter="10" class="top-right-btn mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddArea"
              v-hasPermi="['work:area:add']"
            >保存</el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDeleteArea"
              v-hasPermi="['work:area:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExportArea"
              v-hasPermi="['work:area:export']"
            >导出</el-button>
          </el-col>
        </el-row>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="区域名称" prop="areaName">
            <el-input
              v-model="queryParams.areaName"
              placeholder="请输入区域名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="种植面积" prop="areaRegion">
            <el-input
              v-model="queryParams.areaRegion"
              placeholder="请输入种植面积"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker clearable
                            v-model="queryParams.createTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择创建时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryArea">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>



        <el-table v-loading="loading" :data="areaList" @selection-change="handleSelectionChangeArea">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="区域ID" align="center" prop="areaId"  @click.native="addLineArea()" v-if="false"/>
          <el-table-column label="区域名称" align="center" prop="areaName" >
            <template scope="scope">
              <el-input v-model="scope.row.areaName" placeholder="请输入区域名称" @click.native="addLineArea()"/>
            </template>
          </el-table-column>
          <el-table-column label="种植面积" align="center" prop="areaRegion" >
            <template scope="scope">
              <el-input v-model="scope.row.areaRegion" placeholder="请输入面积" />
            </template>
          </el-table-column>
          <el-table-column label="种植品种" align="center" prop="variety" >
            <template scope="scope">
              <el-select v-model="scope.row.variety" filterable placeholder="请选择">
                <el-option
                  v-for="item in optionsVariety"
                  :key="item.varietyId"
                  :label="item.varietyName"
                  :value="item.varietyId">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否启用" align="center" prop="state" >
            <template scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-value="1"
                inactive-value="2"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template scope="scope">
              <el-date-picker clearable
                              v-model="scope.row.createTime"
                              type="datetime"
                              value-format="yyyy-MM-ss hh:mm:ss"
                              placeholder="请选择创建时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleNewUpdateArea(scope.row)"
                v-hasPermi="['work:area:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleRowDeleteArea(scope.$index,scope.row)"
                v-hasPermi="['work:area:remove']"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getListArea"
        />

      </div>
    </el-tab-pane>
    <el-tab-pane label="产量产能管理" name="third">
      <div class="app-container">
        <el-row :gutter="10" class="top-right-btn mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddPro"
              v-hasPermi="['work:productivity:add']"
            >保存</el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDeletePro"
              v-hasPermi="['work:productivity:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExportPro"
              v-hasPermi="['work:productivity:export']"
            >导出</el-button>
          </el-col>

        </el-row>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="产量" prop="productivity">
            <el-input
              v-model="queryParams.productivity"
              placeholder="请输入产量"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="产能" prop="capacity">
            <el-input
              v-model="queryParams.capacity"
              placeholder="请输入产能"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker clearable
                            v-model="queryParams.createTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择创建时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryPro">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>



        <el-table v-loading="loading" :data="productivityList" @selection-change="handleSelectionChangePro">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="产量id" align="center" prop="productivityId" v-if="false"/>
          <el-table-column label="区域" align="center" prop="area" >
            <template scope="scope">
              <el-select v-model="scope.row.area" filterable placeholder="请选择" @click.native="addLinePro()">
                <el-option
                  v-for="item in optionsArea"
                  :key="item.areaId"
                  :label="item.areaName+'/'+item.areaRegion+'/'+item.variety"
                  :value="item.areaId">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="产量" align="center" prop="productivity" >
            <template scope="scope">
              <el-input v-model="scope.row.productivity" placeholder="请输入产量" />
            </template>
          </el-table-column>
          <el-table-column label="产能" align="center" prop="capacity" >
            <template scope="scope">
              <el-input v-model="scope.row.capacity" placeholder="请输入产能" />
            </template>
          </el-table-column>
          <el-table-column label="时间" align="center" prop="createTime" width="180">
            <template scope="scope">
              <el-date-picker clearable
                              v-model="scope.row.createTime"
                              value-format="yyyy-MM-dd hh:mm:ss"
                              type="datetime"
                              placeholder="请选择时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleNewUpdatePro(scope.row)"
                v-hasPermi="['work:productivity:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleRowDeletePro(scope.$index, scope.row)"
                v-hasPermi="['work:productivity:remove']"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getListPro"
        />

      </div>
    </el-tab-pane>
    <el-tab-pane label="产量数据统计" name="fourth">
      <DataPro></DataPro>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
import DataPro from "@/views/work/data"
import { listVariety,listVarietyAll, getVariety, delVariety, addVariety, updateVariety,addVarietyAll} from "@/api/work/variety";
import { listArea,listAreaAll, getArea, delArea, addArea, updateArea,addAreaAll } from "@/api/work/area";
import { listProductivity, getProductivity, delProductivity, addProductivity, updateProductivity,addProAll } from "@/api/work/productivity";

export default {
  name: "Variety",
  components:{DataPro},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: 'first',
      // 选中数组
      ids: [],
      idsArea: [],
      idsPro: [],
      // 非单个禁用
      singleArea: true,
      singlePro: true,
      single: true,
      // 非多个禁用
      multiple: true,
      multipleArea: true,
      multiplePro: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 品种管理表格数据
      varietyList: [],
      // 品种管理表格数据
      varietyNewList: [],
      optionsVariety:[],
      areaList: [],
      areaNewList: [],
      optionsArea:[],
      // 产量产能管理表格数据
      productivityList: [],
      productivityNewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        varietyName: null,
        varietyNum: null,
        areaName: null,
        areaRegion: null,
        variety: null,
        state: null,
        productivity: null,
        capacity: null,
        area: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListArea();
    this.getListAll();
    this.getListAreaAll();
    this.getListPro();
  },
  methods: {
    tabClick(tab, event) {
      console.log(tab, event);
      // this.$router.push({path:'/'})
      // this.@$router.replace({name:'productivity',params:''});
      // this.$router.push({ path: "/system/user" },tab.);
    },
    /** 查询品种管理列表 */
    getList() {
      this.loading = true;
      listVariety(this.queryParams).then(response => {
        this.varietyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询品种管理列表 */
    getListAll() {
      this.loading = true;
      listVarietyAll().then(response => {
        this.optionsVariety = response.rows;
        this.loading = false;
      });
    },
    /** 查询区域管理列表 */
    getListArea() {
      this.loading = true;
      listArea(this.queryParams).then(response => {
        this.areaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询区域管理列表 */
    getListAreaAll() {
      listAreaAll().then(response => {
        this.optionsArea = response.rows;
      });
    },
    /** 查询产量产能管理列表 */
    getListPro() {
      this.loading = true;
      listProductivity(this.queryParams).then(response => {
        this.productivityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        varietyId: null,
        varietyName: null,
        varietyNum: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQueryArea() {
      this.queryParams.pageNum = 1;
      this.getListArea();
    },
    /** 搜索按钮操作 */
    handleQueryPro() {
      this.queryParams.pageNum = 1;
      this.getListPro();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.varietyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.varietyNewList=selection
    },
    // 多选框选中数据
    handleSelectionChangeArea(selection) {
      this.idsArea = selection.map(item => item.areaId)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.areaNewList=selection
    },
    // 多选框选中数据
    handleSelectionChangePro(selection) {
      this.idsPro = selection.map(item => item.areaId)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.productivityNewList=selection
    },
    /** 批量保存按钮操作 */
    handleAdd() {
      console.log(this.varietyNewList);
      addVarietyAll(this.varietyNewList).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const varietyId = row.varietyId || this.ids
      getVariety(varietyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改品种管理";
      });
    },
    /** 批量保存区域按钮操作 */
    handleAddArea() {
      console.log(this.areaNewList);
      addAreaAll(this.areaNewList).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getListArea();
      });
    },
    /** 修改区域按钮操作 */
    handleNewUpdateArea(row) {
      console.log(row)
      this.reset();
      const areaId = row.areaId
      if (areaId != null) {
        updateArea(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getListArea();
        });
      }
    },
    /** 批量保存产量按钮操作 */
    handleAddPro() {
      console.log(this.productivityNewList);
      addProAll(this.productivityNewList).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getListArea();
      });
    },
    /** 修改区域按钮操作 */
    handleNewUpdatePro(row) {
      console.log(row)
      this.reset();
      const productivityId = row.productivityId
      if (productivityId != null) {
        updateProductivity(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getListPro();
        });
      }
    },
    /** 修改按钮操作 */
    handleNewUpdate(row) {
      console.log(row)
      this.reset();
      const varietyId = row.varietyId
      if (varietyId != null) {
        updateVariety(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.varietyId != null) {
            updateVariety(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVariety(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      const varietyIds = this.ids;
      this.$modal.confirm('是否确认保存品种管理编号为"' + varietyIds + '"的数据项？').then(function() {
        return delVariety(varietyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("保存成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/variety/export', {
        ...this.queryParams
      }, `variety_${new Date().getTime()}.xlsx`)
    },
    /** 删除按钮操作 */
    handleDeleteArea(row) {
      const areaIds = row.areaId || this.ids;
      this.$modal.confirm('是否确认删除区域管理编号为"' + areaIds + '"的数据项？').then(function() {
        return delArea(areaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExportArea() {
      this.download('work/area/export', {
        ...this.queryParams
      }, `area_${new Date().getTime()}.xlsx`)
    },
    /** 删除按钮操作 */
    handleDeletePro(row) {
      const productivityIds = row.productivityId || this.ids;
      this.$modal.confirm('是否确认删除产量产能管理编号为"' + productivityIds + '"的数据项？').then(function() {
        return delProductivity(productivityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExportPro() {
      this.download('work/productivity/export', {
        ...this.queryParams
      }, `productivity_${new Date().getTime()}.xlsx`)
    },

    handleCurrentChange(row, event, column) {
      console.log(row, event, column, event.currentTarget)
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleRowDelete(index, row) {
      console.log(index, row);
      this.varietyList.splice(index, 1);
    },
    handleRowDeleteArea(index, row) {
      console.log(index, row);
      this.areaList.splice(index, 1);
    },
    handleRowDeletePro(index, row) {
      console.log(index, row);
      this.productivityList.splice(index, 1);
    },
    /* 新增一行 */
    addLine(){
      this.varietyList.push({
        line:++this.i
      })
    },
    /* 新增一行 */
    addLinePro(){
      this.productivityList.push({
        line:++this.i
      })
    },
    /* 新增一行 */
    addLineArea(){
      this.areaList.push({
        line:++this.i
      })
    }
  }
};
</script>
